package com.kjj.gift.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.kjj.gift.dto.TenantEnterDto;
import com.kjj.gift.exception.BusinessException;
import com.kjj.gift.service.ITenantService;
import com.kjj.gift.domain.Tenant;
import com.baomidou.mybatisplus.plugins.Page;
import com.kjj.gift.query.TenantQuery;
import com.kjj.gift.util.AjaxResult;
import com.kjj.gift.util.PageList;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.support.PagedListHolder;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/tenant")
public class TenantController {
    @Autowired
    public ITenantService tenantService;


    /**
     * 保存和修改公用的
     * @param tenant  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Tenant tenant){
        try {
            if( tenant.getId()!=null)
                tenantService.updateById(tenant);
            else
                tenantService.insert(tenant);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
                tenantService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id)
    {
        try {

            Tenant tenant = tenantService.selectById(id);
            return AjaxResult.me().setResultObj(tenant);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult list(){

        try {
            System.out.println(11111+"===============================");
            List<Tenant> list = tenantService.selectList(null);
            list.forEach(System.out::println);
            return AjaxResult.me().setResultObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */

    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public AjaxResult json(@RequestBody TenantQuery query)
    {
       /* // 没有关联查询的高级查询
        EntityWrapper<Tenant> wrapper = new EntityWrapper<>();

        if (StringUtils.isNotBlank(query.getKeyword())) {
            wrapper.like("company_name", query.getKeyword())
                    .or()
                    .like("address", query.getKeyword());
        }

        Page<Tenant> page = new Page<Tenant>(query.getPage(),query.getRows());
        page = tenantService.selectPage(page,wrapper);
        PageList pageList = new PageList<Tenant>(page.getTotal(),page.getRecords());*/
        //方案二 高级查询+分页 有关联查询操作
        return AjaxResult.me().setResultObj(tenantService.selectPageList(query));
    }
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody  @Valid TenantEnterDto tenantEnterDto){
        /*try {   方案一使用自定义异常类来捕获 预知的异常
            return tenantService.settlement(tenantEnterDto);
        }
        catch (BusinessException e){
            log.error("Error occurred!"+e.getMessage());
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
        catch (Exception e) {
//            e.printStackTrace();
            log.error("Error occurred!"+e.getMessage());
            return AjaxResult.me().setSuccess(false).setMessage("系统异常！请稍后重试");
        }*/
        return tenantService.settlement(tenantEnterDto); //方案二 使用advice来捕获异常省略大量try/catch代码
    }
}
